import EventID from "../Common/EventID";
import Message from "../Common/Message";
import { IMessage } from "../Interface/IMessage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BaseSprite extends cc.Component {

    private messages = [];

    protected onLoad() { }

    protected start() { }

    protected onDestroy() {
        this.removeMessage();
    }

    protected onEnable() { }
    protected onDisable() { }

    public addMessage(id: EventID, message: IMessage) {
        this.messages.push([id, message]);
        Message.instance.add(id, message);
    }

    public removeMessage() {
        for (let i = 0; i < this.messages.length; i++) {
            if (this.messages[i][1] === this) {
                Message.instance.remove(this.messages[i][0], this.messages[i][1]);
            }
        }
    }
}